package acom.test.swing.aa;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;



public class MyPanel extends JPanel implements Observer{
	
	private static final long serialVersionUID = 1L;
	
	public static int position_x =20;
	public static int position_y =20;
	
	private JLabel name_lab;
	private JButton run_but;
	private JButton stop_but;
	private JButton quit_but;
	
	private JButton in_but;
	private JButton set_but;
	
	private RunPanel run_panel;	
	private JTextArea textarea;
	
	private JFileChooser fileChooser ;
	private PrizeJFrame prizeJFrame;
	private JComboBox comboBox;
	
	private JLabel choosePrize_lab;
	
	private JLabel prizeName_lab;
	
	static List<AnswerPeople> peoples ;
	static List<Prize> prizes;

	public MyPanel(){
		
		peoples=SystemUtil.getPeoplelist();
		prizes=SystemUtil.getPrizelist();
		
		setLayout(null);
		name_lab = new JLabel();		
		name_lab.setText("测试的看看");
		name_lab.setFont(new Font("宋体",Font.PLAIN,32));
		
		prizeName_lab = new JLabel("现在抽的奖项:");
		choosePrize_lab =new JLabel("选择奖项:");
		
        run_but=new JButton("开始");
        stop_but=new JButton("停止");
        quit_but=new JButton("退出");
        
        in_but = new JButton("导入文件");
		set_but = new JButton("设置奖项");
		
		fileChooser = new JFileChooser("D:\\");
		prizeJFrame = new PrizeJFrame();
		prizeJFrame.addObserver(this);
		
		comboBox = new JComboBox();
        
        run_but.setToolTipText("这个按钮是一个开始按钮");//设置按钮组件的工具提示功能
        stop_but.setToolTipText("这个按钮是一个停止按钮");
        quit_but.setToolTipText("这个按钮是一个退出程序按钮");
                                      
        /****  显示结果面板  ****/
     	textarea = new JTextArea();
     	textarea.append("中奖结果："+ "\r\n");
        JScrollPane scrollPane = new JScrollPane(textarea);
		scrollPane.setPreferredSize(new Dimension(350, 300));
		scrollPane.setBounds(position_x+200,position_y+230, 180, 150);
		/****  显示结果面板  ****/
		
		 /****  运行面板  ****/
        run_panel = new RunPanel(textarea);       
        run_panel.setBounds(position_x,position_y+230, 180, 100);                
        /****  运行面板  ****/
                		
        //设置位置 大小
        name_lab.setBounds(position_x+80,position_y, 200, 50);
        
        in_but.setBounds(position_x,position_y+60, 90, 30);
        set_but.setBounds(position_x+120,position_y+60, 90, 30);
        
        choosePrize_lab.setBounds(position_x, position_y+100, 120, 30);
        comboBox.setBounds(position_x, position_y+130, 120, 30);
        
        prizeName_lab.setBounds(position_x, position_y+180, 250, 30);
               
        run_but.setBounds(position_x,position_y+420, 80, 30);
        stop_but.setBounds(position_x+100,position_y+420, 80, 30);
        quit_but.setBounds(position_x+200,position_y+420, 80, 30);
        
       
        //添加 事件
        buileActionListener();
        
        
        //添加组件
        this.add(name_lab);
        this.add(in_but);
        this.add(set_but);
        
        this.add(choosePrize_lab);
        this.add(comboBox);
        this.add(prizeName_lab);
        
        this.add(run_but);
        this.add(stop_but);
        this.add(quit_but);
        
        this.add(run_panel);
        this.add(scrollPane);
	}
	
	public void buileActionListener(){
		
		in_but.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				//导入抽奖人员数据源
				DataSource_Action(e);				
			}			
		});
		set_but.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				//设置奖项
				prizeJFrame.show();
			}			
		});
		
		run_but.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				if(!run_panel.isRunning()){
					run_panel.start();
				}
			}			
		});
		
		stop_but.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				if(run_panel.isRunning()){
					run_panel.stop();
				}
			}			
		});
		
		quit_but.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				System.exit(0);
			}			
		});
		
		//下拉框 事件
		comboBox.addItemListener( new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent e) {
				itemStateChanged_Action(e);
			}			
		});
	}
	
	protected void itemStateChanged_Action(ItemEvent e) {
		if (e.getStateChange() == ItemEvent.SELECTED) 
		{
			String prizeName=(String) e.getItem();						
			Prize prize = SystemUtil.findPrizeByName(prizeName);
			
			prizeName_lab.setText("现在抽的奖项:" +prize.getPname() +" , 奖项数："+prize.getTotal());
			
			SystemUtil.setCurPrize(prize);
		}
	}

	protected void DataSource_Action(MouseEvent e) {
		fileChooser.setApproveButtonText("确定");
		fileChooser.setDialogTitle("打开文件");
		File file = null;
		int result = fileChooser.showOpenDialog(this);

		if (result == JFileChooser.APPROVE_OPTION) {
			file = fileChooser.getSelectedFile();
			System.out.println("您导入的文件：" + file.getName());
		} else if (result == JFileChooser.CANCEL_OPTION) {
			System.out.println("您没有导入任何文件");
		}

		if (file != null) {
			peoples = ExcelUtil.readExcelFile(file);
			SystemUtil.setPeoplelist(peoples);
			
			if (peoples.size() > 1) {
				/*for (AnswerPeople p : peoples) {
					textarea.append(p.getPeopleName() + "\r\n");
					textarea.append(p.getPeoTel() + "\r\n");
					textarea.append(p.getPeoCorpType() + "\r\n");
					textarea.append(p.getAddress() + "\r\n");
					textarea.append("=======>" + "\r\n");
				}*/
			} else {
				System.out.println("没有读取到excel的内容");
			}
		}
	}

	//观察者模式 ：观察到了 增加奖项后，更新下拉框内容
	@Override
	public void update(Observable o, Object arg) {
		
		//更新下拉框内容
		List<Prize> prizelist=((PrizeJFrame)o).getPrizelist();
		
		comboBox.removeAllItems();
		for(Prize prize:prizelist){
			comboBox.addItem(prize.getPname());
		}
		
	}
	
	private void insertComboBox(String item){
		comboBox.addItem(item);
	}
}
